package com.budwk.app.sqfwV2.generator.centuryCity;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityOldCarePdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.ProblemParamDTO;
import com.budwk.app.sqfwV2.enums.centuryCityOldCare.CenturyCityOldCareV2FileEnums;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-06-06
 * <p>
 * 世纪城养老服务站巡查1.1 CenturyCityOldCare
 * 观山湖区餐饮服务日常巡查表.docx
 */
public class ElderlyCareInsSafetyCheckPdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        return true;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return CenturyCityOldCareV2FileEnums.elderlyCareInsSafetyCheckPdf.getPath();
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        CenturyCityOldCarePdfDTO fieldDTO = (CenturyCityOldCarePdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        // 整改措施
        String rectificationMeasure = "";
        StringBuilder rmSb = new StringBuilder();

        List<String> riskImageList = Lists.newArrayList();
        List<String> rectificationAfterImageList = Lists.newArrayList();

        if (CollectionUtil.isNotEmpty(fieldDTO.getCheckItemList())) {
            for (ProblemParamDTO problemParamDTO : fieldDTO.getCheckItemList()) {
                if (StringUtils.isNotBlank(problemParamDTO.getRectificationMeasure())) {
                    rmSb.append(problemParamDTO.getRectificationMeasure()).append(";");

                    String riskImages = problemParamDTO.getRiskImages();
                    if (StringUtils.isNotBlank(riskImages)) {
                        riskImageList.addAll(Lists.newArrayList(Splitter.on(",").split(riskImages)));
                    }

                    String rectificationAfterPhoto = problemParamDTO.getRectificationAfterPhoto();
                    if (StringUtils.isNotBlank(rectificationAfterPhoto)) {
                        rectificationAfterImageList.addAll(Lists.newArrayList(Splitter.on(",").split(rectificationAfterPhoto)));
                    }
                }
            }
        }
        if (StringUtils.isBlank(rmSb.toString())) {
            rectificationMeasure = "没有安全隐患";
        } else {
            rectificationMeasure = rmSb.toString();
        }
        paramMap.put("rectificationMeasure", rectificationMeasure);
        paramMap.put("fillDateStr", DateUtil.format(pdfGenerateParam.getBusinessParam().getFillDate(), "yyyy年MM月dd日"));
        paramMap.put("inspectionPhotos", CommonFormOfficeUtils.addWordPicture(fieldDTO.getInspectionPhotos(), 5.0f));
        paramMap.put("businessStatus", fieldDTO.getBusinessStatus());

        String imagesDesc = "", imagesLastDesc = "";
        if (riskImageList.size() > 0) {
            imagesDesc = StrFormatter.format("{}消防安全问题整改前照片", DateUtil.format(fieldDTO.getInspectionDate(), "yyyy年MM月dd日"));
        }
        paramMap.put("imagesDesc", imagesDesc);
        paramMap.put("images", CommonFormOfficeUtils.addWordPicture(riskImageList, "pictures", 5f));


        if (rectificationAfterImageList.size() > 0) {
            imagesLastDesc = StrFormatter.format("{}消防安全问题整改后照片", DateUtil.format(fieldDTO.getInspectionDate(), "yyyy年MM月dd日"));
        }

        paramMap.put("imagesLastDesc", imagesLastDesc);
        paramMap.put("imagesLast", CommonFormOfficeUtils.addWordPicture(rectificationAfterImageList, "pictures", 5f));

    }
}
